package com.zhonghui.scm.service.impl;

import com.zhonghui.scm.domain.ScmPurchaseApply;
import com.zhonghui.scm.domain.ScmPurchaseArrival;
import com.zhonghui.scm.domain.ScmPurchaseArrivalItem;
import com.zhonghui.scm.domain.ScmPurchaseContract;
import com.zhonghui.scm.mapper.ScmPurchaseArrivalMapper;
import com.zhonghui.scm.mapper.ScmPurchaseContractMapper;
import com.zhonghui.scm.service.IScmPurchaseArrivalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Formatter;

/**
 * 采购到货Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Service
public class ScmPurchaseArrivalServiceImpl implements IScmPurchaseArrivalService
{
    @Autowired
    private ScmPurchaseArrivalMapper scmPurchaseArrivalMapper;

    /**
     * 查询采购到货
     *
     * @param id 采购到货主键
     * @return 采购到货
     */
    @Override
    public ScmPurchaseArrival getById(Long id)
    {
        return scmPurchaseArrivalMapper.getById(id);
    }

    /**
     * 查询采购到货列表
     *
     * @param scmPurchaseArrival 采购到货
     * @return 采购到货
     */
    @Override
    public List<ScmPurchaseArrival> getList(ScmPurchaseArrival scmPurchaseArrival)
    {
        return scmPurchaseArrivalMapper.getList(scmPurchaseArrival);
    }

    /**
     * 新增采购到货
     *
     * @param scmPurchaseArrival 采购到货
     * @return 结果
     */
    @Override
    @Transactional
    public int add(ScmPurchaseArrival scmPurchaseArrival)
    {

        scmPurchaseArrival.setDeliveryNumber(generateNumber("CGDH"));
        int row = scmPurchaseArrivalMapper.add(scmPurchaseArrival);
        insertItem(scmPurchaseArrival);
        return row;
    }

    /**
     * 修改采购到货
     *
     * @param scmPurchaseArrival 采购到货
     * @return 结果
     */
    @Override
    public int edit(ScmPurchaseArrival scmPurchaseArrival)
    {
        scmPurchaseArrivalMapper.delItem(scmPurchaseArrival.getId());
        insertItem(scmPurchaseArrival);
        return scmPurchaseArrivalMapper.edit(scmPurchaseArrival);
    }

    /**
     * 批量删除采购到货
     *
     * @param ids 需要删除的采购到货主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmPurchaseArrivalMapper.delItem(id);
        }
        return scmPurchaseArrivalMapper.del(ids);
    }


    public void insertItem(ScmPurchaseArrival scmPurchaseArrival){
        List<ScmPurchaseArrivalItem> scmPurchaseArrivalItemList = scmPurchaseArrival.getScmPurchaseArrivalItemList();

        if (!scmPurchaseArrivalItemList.isEmpty()){

        for (ScmPurchaseArrivalItem scmPurchaseArrivalItem : scmPurchaseArrivalItemList) {
            scmPurchaseArrivalItem.setPurchasingArrivalId(scmPurchaseArrival.getId());
        }
        scmPurchaseArrivalMapper.addItem(scmPurchaseArrivalItemList);

        }
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
}
